package com.jcodecraeer.xrecyclerview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by meili on 2016/12/10.
 */

public class TweenAnimationRefreshLayout extends LinearLayout implements BaseRefreshHeader {

    public int mMeasuredHeight;
    private View mContainer;
    private ImageView image_header;
    private int mState = STATE_NORMAL;
    private AnimationDrawable animationDrawable;

    public TweenAnimationRefreshLayout(Context context) {
        super(context);
        initView();
    }

    public TweenAnimationRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public TweenAnimationRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    public int getState() {
        return mState;
    }

    private void initView() {
        mContainer = LayoutInflater.from(getContext()).inflate(R.layout.layout_header, null);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 0, 0);
        this.setLayoutParams(lp);
        this.setPadding(0, 0, 0, 0);
        addView(mContainer, new LayoutParams(LayoutParams.MATCH_PARENT, 0));
        setGravity(Gravity.BOTTOM);
        image_header = (ImageView) findViewById(R.id.pull_to_refresh_image);

        image_header.setImageResource(R.drawable.animation);
        measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mMeasuredHeight=150;
        animationDrawable = (AnimationDrawable) image_header.getDrawable();
    }

    //获得可见高度
    public int getVisibleHeight() {
        LayoutParams lp = (LayoutParams) mContainer.getLayoutParams();
        return lp.height;
    }

    //设置可见高度
    public void setVisibleHeight(int height) {
        if (height < 0) height = 0;
        LayoutParams lp = (LayoutParams) mContainer.getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
    }

    @Override
    public void onMove(float delta) {
        if (getVisibleHeight() > 0 || delta > 0) {
            setVisibleHeight((int) delta + getVisibleHeight());
            if (mState <= STATE_RELEASE_TO_REFRESH) { // 未处于刷新状态，更新箭头
                if (getVisibleHeight() > mMeasuredHeight) {
                    setState(STATE_RELEASE_TO_REFRESH);
                } else {
                    setState(STATE_NORMAL);
                }
            }
        }
    }

    public void setState(int state) {
        if (state == mState) return;
        if (state == STATE_REFRESHING) { //正在刷新
            image_header.setVisibility(VISIBLE);
            animationDrawable.start();
            smoothScrollTo(mMeasuredHeight);
        } else if (state == STATE_DONE) {
            animationDrawable.stop();
        } else {
            // 显示箭头图片
            animationDrawable.stop();
        }

        switch (state) {
            case STATE_NORMAL:
                animationDrawable.stop();
                break;
            case STATE_REFRESHING:
                animationDrawable.start();
                break;
            case STATE_DONE:
                animationDrawable.stop();
                break;
            default:
        }

        mState = state;
    }

    @Override
    public boolean releaseAction() {

        boolean isOnRefresh = false;
        int height = getVisibleHeight();
        if (height == 0) // not visible.
            isOnRefresh = false;

        if (getVisibleHeight() > mMeasuredHeight && mState < STATE_REFRESHING) {
            setState(STATE_REFRESHING);
            isOnRefresh = true;
        }
        // refreshing and header isn't shown fully. do nothing.
        if (mState == STATE_REFRESHING && height <= mMeasuredHeight) {
            //return;
        }
        if (mState != STATE_REFRESHING) {
            smoothScrollTo(0);
        }

        if (mState == STATE_REFRESHING) {
            int destHeight = mMeasuredHeight;
            smoothScrollTo(destHeight);
        }
        return isOnRefresh;
    }

    @Override
    public void refreshComplete() {
        setState(STATE_DONE);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                reset();
            }
        }, 200);
    }

    public void reset() {
        smoothScrollTo(0);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                setState(STATE_NORMAL);
            }
        }, 500);
    }

    private void smoothScrollTo(int destHeight) {
        ValueAnimator animator = ValueAnimator.ofInt(getVisibleHeight(), destHeight);
        animator.setDuration(300).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setVisibleHeight((int) animation.getAnimatedValue());
            }
        });
        animator.start();
    }
}
